//
//  QZMeViewController.m
//  WeiWeiHaircut
//
//  Created by 上官惠阳 on 15/12/9.
//  Copyright © 2015年 超群. All rights reserved.
//

#import "QZMeViewController.h"
#import "QZMeHeadView.h"
#import "MeCell.h"
#import "QZUserModel.h"
#import "UIButton+WebCache.h"
#import "MyOrdersViewController.h"
#import "QZUserMsgViewController.h"
#import "QZMyBalanceController.h"
#import "QZAddressController.h"
#import "QZMyAppointVc.h"

#define HEADHEIGHT 140
@interface QZMeViewController ()

@end

@implementation QZMeViewController
{
    QZUserModel *_meModel;
    NSArray *_imageArr;
    QZMeHeadView *_headView;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    

   UIImage *image =  [[TMCache sharedCache] objectForKey:@"headImage"];
    if (image) {
        [_headView.headerImageView setBackgroundImage:image forState:UIControlStateNormal];
    }
    NSString *nickName = [[TMCache sharedCache] objectForKey:@"nickName"];
    if (nickName) {
        _headView.userNameLabel.text = nickName;
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleWithSting:@"我"];
    
    [self createTableViewWithPlainStyle:YES andSeparatorStyleNone:YES andBackGroudImageName:nil];
    self.tableView.backgroundColor = BACKGROUDCOLOR;
    
    [self registCellWithNib:@"MeCell" addIdentifier:@"meCell" addHeight:44];
    
    [self createHeadView];
    
    [self loadData];
}
-(void)createHeadView
{
    _headView = [[[NSBundle mainBundle]loadNibNamed:@"QZMeHeadView" owner:self options:nil]lastObject];
    _headView.frame = CGRectMake(0, 0, WIDTH, HEADHEIGHT);
    [_headView.headerImageView setBackgroundImage:[[TMCache sharedCache] objectForKey:@"headImage"] forState:UIControlStateNormal];
    [self.view addSubview:_headView];
    [_headView.headerImageView addTarget:self action:@selector(headAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView.frame = CGRectMake(0, HEADHEIGHT, WIDTH, HEIGHT - HEADHEIGHT);

    NSString *imageURL = [[TMCache sharedCache] objectForKey:@"headImageURL"];
    if (imageURL) {
          [_headView.headerImageView sd_setBackgroundImageWithURL:[NSURL URLWithString:imageURL] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"1123.jpg"]];
    }
    
    NSString *nickName = [[TMCache sharedCache] objectForKey:@"nickName"];
    if (nickName) {
        _headView.userNameLabel.text = nickName;
    }

    [self downloadData];
}
-(void)downloadData{
    
    
            [self requestWithKeys:@[@"user_id"] andValues:@[@([[QZUserInfo userId] intValue])] andUrlKey:@"User.GetBaseInfo" isCache:YES];
}
-(void)headAction:(UIButton *)btn
{
    QZUserMsgViewController* userVc = [[QZUserMsgViewController alloc]init];
    userVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:userVc animated:YES];
}
-(void)loadData
{
    [self.tableSource setArray:@[@"我的订单",@"我的预约",@"我的钱包",@"收货地址"]];
    _imageArr = @[@"我的订单.png",@"我的预约.png",@"我的钱包.png",@"收货地址.png"];
    
    [self.tableView reloadData];
}
-(void)request_POST_FinishValue:(id)value tagNSString:(NSString *)tag
{
    NSLog(@"value = %@",value);

    if ([tag isEqualToString:@"User.GetBaseInfo"]) {
        if ([value[@"data"][@"status"] intValue] == 100) {
            _meModel = [[QZUserModel alloc]initWithDictionary:value[@"data"][@"data"] error:nil];
            
            [QZUserInfo keepUserMsgWithUserModel:_meModel];
            //没有数据 并且有头像url zamen
            if (![[TMCache sharedCache] objectForKey:@"headImage"] &&_meModel.userimg) {
                    UIImage *headImg = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_meModel.userimg]]];
                [[TMCache sharedCache] setObject:headImg forKey:@"headImage"];
            }
            _headView.userNameLabel.text = _meModel.nickName;
        }
    }
}
-(void)loadCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    MeCell *meCell = (MeCell *)cell;
    meCell.titleLabel.text    = self.tableSource[indexPath.row];
    meCell.titleImage.image   = [UIImage imageNamed:_imageArr[indexPath.row]];
    meCell.textaLabel.hidden  = YES;
}
-(void)actionAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        MyOrdersViewController* myworkVC =[[MyOrdersViewController alloc]initWithNibName:nil bundle:nil];
        myworkVC.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:myworkVC animated:YES];
    }
    if (indexPath.row == 1) {
        QZMyAppointVc *appointVc = [[QZMyAppointVc alloc]init];
        appointVc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:appointVc animated:YES];
    }
    if(indexPath.row == 2){
        QZMyBalanceController *balance =[[QZMyBalanceController alloc] init];
        balance.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:balance animated:YES];
    }
    if (indexPath.row == 3) {
        QZAddressController *address = [[QZAddressController alloc] initWithNibName:nil bundle:nil];
        address.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:address animated:YES];
    }
}
#pragma mark UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //实现下拉头部放大
    CGFloat Y = self.tableView.contentOffset.y;

    CGFloat proportion = WIDTH / HEADHEIGHT;//宽高比
    if (Y < 0) {
        CGFloat heightOfAdd = -Y;//tableVie往下拉得高度，也就是_headView需要增加的高度
        CGFloat X = (HEADHEIGHT + heightOfAdd) * proportion - WIDTH;//_headView需要增加的宽度
        _headView.frame = CGRectMake(-(X / 2),0,WIDTH + X, HEADHEIGHT + heightOfAdd);
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
